
package userInterface;


/*
 
 public class Singleton {
   private final static Singleton instance= new Singleton();
 
   // Private constructor suppresses generation of a (public) default constructor
   private Singleton() {}
 
   public static Singleton getInstance() {
     return instance;
   }
 }

 * a tutorial reader.
 */

import importer.*;
import persistance.*;

import java.io.*;
import java.net.URL;
import java.util.ArrayList;
import java.awt.*;
import java.awt.event.*;
import java.awt.BorderLayout;
import java.awt.Dimension;
import java.awt.GridLayout;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.awt.event.MouseListener;

import javax.swing.JFileChooser;
import javax.swing.JScrollPane;
import javax.swing.JTabbedPane;
import javax.swing.JComponent;
import javax.swing.JButton;
import javax.swing.JFrame;
import javax.swing.JPanel;
import javax.swing.tree.DefaultMutableTreeNode;
import javax.swing.tree.DefaultTreeModel;
import javax.swing.tree.TreeNode;
import javax.swing.tree.TreePath;
import javax.swing.*;
import javax.swing.border.*;
import javax.swing.JMenu;
import javax.swing.JMenuBar;
import javax.swing.JMenuItem;
import javax.swing.JTextArea; // XML Display
import javax.swing.KeyStroke;

import org.jdom.JDOMException;
import converter.*;

import persistance.Agent;
import persistance.Message;
import persistance.UMLSequenceDomainModeler;

public class MainApplicationFrame extends JPanel 
                             implements ActionListener {
    public static MainApplicationFrame instance = new MainApplicationFrame();
	static final long serialVersionUID = 1;
    public String workingDirectory;
    private String ADD_COMMAND = "add";
    private String REMOVE_COMMAND = "remove";
    private String GENERATE_SEQUENCE = "Sequence Diagram";
    private String LAUNCH_VIOLET = "Launch Violet";
    private String HELP_PAGE ="Help Page"; 
    private String NEW = "New Project";
    private String SAVE = "Save Project";
    private String OPEN = "Open Project";
    private String CLOSE = "Close Project";
    private ArrayList inputTypes;
    
    private static JFrame frame;
    private DynamicTree MaSE_TreePanel;
    private JTabbedPane MaSE_ViewerPanel, UML_ViewerPanel;
    private DynamicTree UML_TreePanel;
    private JPanel  MaSE_Panel, UML_Panel, WorkPanel;
    private JComponent MaSE_TextPanel, MaSE_GfxPanel, UML_TextPanel, UML_GfxPanel; 
    
    private String classDiagramFilepath;
    private JFileChooser fc;
    private JLabel MaSEpicture;
    private JTextArea mumConsole, MaSE_TextPanelTextArea, UML_TextPanelTextArea;
    private JScrollPane consolescrollPane;
    
    DefaultMutableTreeNode p1, p2, p3, p4, p5, p6, p7;
    
    private JMenuItem sequenceMenuItem;
    
    public static MainApplicationFrame getInstance() {
        if (instance == null)	
        	instance = new MainApplicationFrame();
        return instance;
      }

    
    
    public JMenuBar createMenuBar(){
        JMenuBar menuBar;
        JMenu menu, submenu;
        JMenuItem menuItem;
        
        menuBar = new JMenuBar();

        menu = new JMenu("File");
        menu.setMnemonic(KeyEvent.VK_F);
        menuBar.add(menu);
        
        menuItem = new JMenuItem(NEW, KeyEvent.VK_N);
        menuItem.addActionListener(this);
        menu.add(menuItem);
        
        menuItem = new JMenuItem(SAVE, KeyEvent.VK_S);
        menuItem.addActionListener(this);
        menu.add(menuItem);
        
        menuItem = new JMenuItem(OPEN, KeyEvent.VK_O);
        menuItem.addActionListener(this);
        menu.add(menuItem);
        
        menuItem = new JMenuItem("Print", KeyEvent.VK_P);
        menuItem.addActionListener(this);
        menu.add(menuItem);
        
        menuItem = new JMenuItem("Export", KeyEvent.VK_E);
        menuItem.addActionListener(this);
        menu.add(menuItem);
        
        menu.addSeparator();
        menuItem = new JMenuItem(CLOSE, KeyEvent.VK_S);
        menuItem.addActionListener(this);
        menu.add(menuItem);
        //End of File
        
        menu = new JMenu("Generate");
        menu.setMnemonic(KeyEvent.VK_G);
        menuBar.add(menu);
        
        menuItem = new JMenuItem("Class Diagram");
        menuItem.setEnabled(false);
        menuItem.addActionListener(this);
        menu.add(menuItem);
        
        menuItem = new JMenuItem("Deployment Diagram");
        menuItem.setEnabled(false);
        menuItem.addActionListener(this);
        menu.add(menuItem);
        
        menuItem = new JMenuItem("Use Case Diagram");
        menuItem.setEnabled(false);
        menuItem.addActionListener(this);
        menu.add(menuItem);
        
        menuItem = new JMenuItem("Activity Diagram");
        menuItem.setEnabled(false);
        menuItem.addActionListener(this);
        menu.add(menuItem);
        
        menuItem = new JMenuItem("State Machine Diagram");
        menuItem.setEnabled(false);
        menuItem.addActionListener(this);
        menu.add(menuItem);
        
        sequenceMenuItem = new JMenuItem("Sequence Diagram");
        sequenceMenuItem.setEnabled(false);
        sequenceMenuItem.addActionListener(this);
        menu.add(sequenceMenuItem);
        
        menuItem = new JMenuItem("Interaction Overview Diagram");
        menuItem.setEnabled(false);
        menuItem.addActionListener(this);
        menu.add(menuItem);
        
        menu = new JMenu("Help");
        menu.setMnemonic(KeyEvent.VK_G);
        menuBar.add(menu);
        
        menuItem = new JMenuItem("Help Page");
        menuItem.addActionListener(this);
        menu.add(menuItem);
        
        menuItem = new JMenuItem("About");
        menuItem.addActionListener(this);
        menu.add(menuItem);
        
        return menuBar;

    }
    
    protected JComponent makeTextPanel(String text) {
        JPanel panel = new JPanel(false);
        JLabel filler = new JLabel(text);
        filler.setHorizontalAlignment(JLabel.CENTER);
        panel.setLayout(new GridLayout(1, 1));
        panel.add(filler);
        return panel;
    }

    
    private MainApplicationFrame() {
        super(new BorderLayout());

    	inputTypes = new ArrayList();
    	inputTypes.add("Goal Hirearchy");
    	inputTypes.add("Use Case Diagram");
    	inputTypes.add("Sequence Diagrams");
    	inputTypes.add("Concurrent Task Digrams");
    	inputTypes.add("Role Model Diagram");        
    	inputTypes.add("Agent Class Diagram");
    	inputTypes.add("Conversation Diagram");
    	inputTypes.add("Agent Architecture Diagram");
    	inputTypes.add("Deployment Diagram");
        
        //Create the components.
        Border raisedetched = BorderFactory.createEtchedBorder(EtchedBorder.RAISED);
        MaSE_Panel = new JPanel();
        MaSE_TreePanel = new DynamicTree("MaSE Artifacts");
        MaSE_ViewerPanel = new JTabbedPane();
        
        //MaSE_TextPanel = makeTextPanel("THIS WILL BE THE XML VIEW");
        MaSE_TextPanelTextArea = new JTextArea(5,10);
        //MaSE_TextPanel.add(MaSE_TextPanelTextArea);
        MaSE_TextPanelTextArea.setEditable(false);
        JScrollPane MaSE_ScrollPane = new JScrollPane(MaSE_TextPanelTextArea);
        MaSE_ViewerPanel.addTab("XML VIEW", MaSE_ScrollPane);
        
        MaSE_GfxPanel = makeTextPanel("THIS WILL BE THE GRAPHICAL VIEW");

        MaSE_ViewerPanel.addTab("GRAPHICAL VIEW", MaSE_GfxPanel);
        
        UML_Panel = new JPanel();
        UML_TreePanel = new DynamicTree("UML Artifacts");
        UML_ViewerPanel = new JTabbedPane();
       
        
        MouseListener m1 = new MouseAdapter() {
            public void mousePressed(MouseEvent e) {
                int selRow = MaSE_TreePanel.tree.getRowForLocation(e.getX(), e.getY());
                TreePath selPath = MaSE_TreePanel.tree.getPathForLocation(e.getX(), e.getY());
                if(selRow != -1) {
                    if(e.getClickCount() == 1) {
                        //mySingleClick(selRow, selPath); DO Nothing
                    }
                    else if(e.getClickCount() == 2) {
                        myMaSEDoubleClick(selRow, selPath);
                    }
                }
            }
        };
        
        MaSE_TreePanel.tree.addMouseListener(m1);
        
        MouseListener m2 = new MouseAdapter() {
            public void mousePressed(MouseEvent e) {
                int selRow = UML_TreePanel.tree.getRowForLocation(e.getX(), e.getY());
                TreePath selPath = UML_TreePanel.tree.getPathForLocation(e.getX(), e.getY());
                if(selRow != -1) {
                    if(e.getClickCount() == 1) {
                        //mySingleClick(selRow, selPath); DO Nothing
                    }
                    else if(e.getClickCount() == 2) {
                        myUMLDoubleClick(selRow, selPath);
                    }
                }
            }
        };
        
        UML_TreePanel.tree.addMouseListener(m2);
        UML_TextPanelTextArea = new JTextArea(5,10);
        UML_TextPanelTextArea.setEditable(false);
        //UML_TextPanel = makeTextPanel("THIS WILL BE THE XML VIEW");
        JScrollPane UML_ScrollPane = new JScrollPane(UML_TextPanelTextArea);
        UML_ViewerPanel.addTab("XML VIEW", UML_ScrollPane);
        
        UML_GfxPanel = makeTextPanel("THIS WILL BE THE GRAPHICAL VIEW");
        UML_ViewerPanel.addTab("GRAPHICAL VIEW", UML_GfxPanel);
        
        fc = new JFileChooser();
        populateMaSETree(MaSE_TreePanel);
        populateUMLTree(UML_TreePanel);

        JButton addButton = new JButton("Add MaSE Diagram");
        addButton.setActionCommand(ADD_COMMAND);
        addButton.addActionListener(this);
        
        JButton removeButton = new JButton("Remove MaSE Diagram");
        removeButton.setActionCommand(REMOVE_COMMAND);
        removeButton.addActionListener(this);
        
        //JButton launchVButton = new JButton("Launch Violet");
        //launchVButton.setActionCommand(LAUNCH_VIOLET);
        //launchVButton.addActionListener(this);
        

        //Lay everything out.
        
        JPanel TopPanel = new JPanel(new GridLayout(0,2));
        TopPanel.add(addButton,BorderLayout.WEST);
        TopPanel.add(removeButton,BorderLayout.CENTER);
        
        MaSE_ViewerPanel.setBorder(raisedetched);
        MaSE_TreePanel.setPreferredSize(new Dimension(400, 260));
        MaSE_ViewerPanel.setPreferredSize(new Dimension(600, 260));
        MaSE_Panel.add(MaSE_TreePanel, BorderLayout.WEST);
        MaSE_Panel.add(MaSE_ViewerPanel, BorderLayout.EAST);
        
        UML_ViewerPanel.setBorder(raisedetched);
        UML_TreePanel.setPreferredSize(new Dimension(400,260));
        UML_ViewerPanel.setPreferredSize(new Dimension(600, 260));
        UML_Panel.add(UML_TreePanel, BorderLayout.WEST);
        UML_Panel.add(UML_ViewerPanel, BorderLayout.EAST);
        
        WorkPanel = new JPanel(new GridLayout(2,0)); 
        WorkPanel.add(MaSE_Panel, BorderLayout.NORTH);
        WorkPanel.add(UML_Panel, BorderLayout.SOUTH);
        
        add(WorkPanel, BorderLayout.CENTER);
        //add(MaSE_TreePanel, BorderLayout.WEST);
        //add(MaSE_Viewer, BorderLayout.CENTER);
     
        //add(UML_TreePanel,BorderLayout.EAST);
        //add(UML_Viewer);
        
        JPanel Bottompanel = new JPanel(new GridLayout(0,2));
        //Bottompanel.add(launchVButton,BorderLayout.EAST);
        Bottompanel.add(addButton,BorderLayout.WEST);
        Bottompanel.add(removeButton,BorderLayout.EAST);
        add(Bottompanel, BorderLayout.SOUTH);
        
        mumConsole = new JTextArea(5,10);
        mumConsole.setEditable(false);
        consolescrollPane = new JScrollPane(mumConsole);
        
        add(consolescrollPane, BorderLayout.NORTH);
    }

    protected void myUMLDoubleClick(int selRow, TreePath selPath) {
    	if(selPath.getPathCount() > 2){ // If it is not the Root or MaSE Artifact Type
			TreeNode filepath = (TreeNode)selPath.getLastPathComponent();
			try {
				UML_TextPanelTextArea.setText(null);// Empty the old stuff
				BufferedReader inputStream = new BufferedReader(new FileReader(filepath.toString()));
				while(inputStream.ready())
				{
					UML_TextPanelTextArea.append(inputStream.readLine() + "\n");
				}
			} catch (Exception e) {
				// TODO Auto-generated catch block
				e.printStackTrace();
			}

		}
		
	}



	protected void myMaSEDoubleClick(int selRow, TreePath selPath) {
		//mumConsole.append("Selected Row: " + selRow + " SelectedPath: " + selPath );
		if(selPath.getPathCount() > 2){ // If it is not the Root or MaSE Artifact Type
			TreeNode filepath = (TreeNode)selPath.getLastPathComponent();
			try {
				MaSE_TextPanelTextArea.setText(null);// Empty the old stuff
				BufferedReader inputStream = new BufferedReader(new FileReader(filepath.toString()));
				while(inputStream.ready())
				{
					MaSE_TextPanelTextArea.append(inputStream.readLine() + "\n");
				}
			} catch (Exception e) {
				// TODO Auto-generated catch block
				e.printStackTrace();
			}

		}

	}



	protected void mySingleClick(int selRow, TreePath selPath) {
		// TODO Auto-generated method stub
		
	}



	public void populateMaSETree(DynamicTree treePanel) {
        
    	String p1Name = new String("Goal Hirearchy");
        String p2Name = new String("Use Case Diagram");
        String p3Name = new String("Sequence Diagrams");
        String p4Name = new String("Concurrent Task Digrams");
        String p5Name = new String("Role Model Diagram");        
        String p6Name = new String("Agent Class Diagram");
        String p7Name = new String("Conversation Diagram");
        String p8Name = new String("Agent Architecture Diagram");
        String p9Name = new String("Deployment Diagram");
        
  
        DefaultMutableTreeNode p1, p2, p3, p4, p5, p6, p7, p8, p9;
        p1 = treePanel.addObject(null, p1Name);
        p2 = treePanel.addObject(null, p2Name);
        p3 = treePanel.addObject(null, p3Name);
        p4 = treePanel.addObject(null, p4Name);
        p5 = treePanel.addObject(null, p5Name);
        p6 = treePanel.addObject(null, p6Name);
        p7 = treePanel.addObject(null, p7Name);
        p8 = treePanel.addObject(null, p8Name);
        p9 = treePanel.addObject(null, p9Name);
    }
    
    
    public void populateUMLTree(DynamicTree treePanel){
    	String p1Name = new String("Class Diagram");
        String p2Name = new String("Deployment Diagram");
        String p3Name = new String("Use Case Diagram");
        String p4Name = new String("Activity Diagram");
        String p5Name = new String("State Machine Diagram");
        String p6Name = new String("Sequence Diagram");
        String p7Name = new String("Interaction Overview");


        p1 = treePanel.addObject(null, p1Name);
        p2 = treePanel.addObject(null, p2Name);
        p3 = treePanel.addObject(null, p3Name);
        p4 = treePanel.addObject(null, p4Name);
        p5 = treePanel.addObject(null, p5Name);
        p6 = treePanel.addObject(null, p6Name);
        p7 = treePanel.addObject(null, p7Name);

    }
    
    public void actionPerformed(ActionEvent e) {
        String command = e.getActionCommand();
       
        if (ADD_COMMAND.equals(command)) {
            //Better if done like a switch, this works for now though
        	if (inputTypes.contains(MaSE_TreePanel.getCurrentlySelectedNode())) {
                int returnVal = fc.showOpenDialog(MainApplicationFrame.this);
                if (returnVal == JFileChooser.APPROVE_OPTION) {
                	File file = fc.getSelectedFile();
                	DefaultMutableTreeNode me = MaSE_TreePanel.addObject(file.getAbsolutePath());
                	if(MaSE_TreePanel.getCurrentlySelectedNode().equals("Agent Class Diagram")){
                		sequenceMenuItem.setEnabled(true);
                		classDiagramFilepath = file.getAbsolutePath();
                	}
                		
                }	
        	}
        	else
        		mumConsole.append("Not a valid bucket");
        		
        		        	
        } 
        else if (REMOVE_COMMAND.equals(command)) {
        	//Need to add ProtectionCode here.
            MaSE_TreePanel.removeCurrentNode();
        }
        else if (HELP_PAGE.equals(command))
        {
        	BrowserController control = new BrowserController();
        	control.openUrl("http://mas-uml-modeler.googlecode.com/files/MUM_Manual3.0.mht");
        	
        }
        
        else if (GENERATE_SEQUENCE.equals(command)) {
        	if(! MaSE_TreePanel.getParentOfCurrentlySelectedNode().equals("Sequence Diagrams"))
        		mumConsole.append("You have not selected a sequence diagram to Convert");
        	else {
        		File umlsequence = generateSequence(MaSE_TreePanel.getCurrentlySelectedNode());
        	}
        }
        else if(NEW.equals(command))
        {
        	String[] args = new String[2];
        	args[1] = workingDirectory;
        	frame.setVisible(false);
        	instance = null;
        	ApplicationController ac = new ApplicationController();
        	ac.main(args);
        }
        else if(SAVE.equals(command))
        {
        	SaveProject();
        }
        else if(OPEN.equals(command))
        {
        	OpenProject();
        }
        else if(CLOSE.equals(command))
        {
        	System.exit(0);
        }
        else
        {
            JMenuItem source = (JMenuItem)(e.getSource());
            String s = "Item event detected."
                       + "\n"
                       + "Event source: " + source.getText();
            mumConsole.append(s + "\n");
            mumConsole.append(workingDirectory);
        }
    }
    
   
    
    private void SaveProject()
    {
    	FileOutputStream fos = null;
    	ObjectOutputStream out = null;
    	try
    	{
    	   
	       fc = new JFileChooser();
	       fc.setFileSelectionMode(JFileChooser.FILES_ONLY);
	       fc.setName("Save To File");
	       File filePath;
	       int returnVal = fc.showDialog(this , "Save To File");
	       if (returnVal == JFileChooser.APPROVE_OPTION){
	    	   filePath = fc.getSelectedFile();
			   fos = new FileOutputStream(filePath.getAbsolutePath() +".MaSE" );
		       out = new ObjectOutputStream(fos);
		       out.writeObject(MaSE_TreePanel.treeModel);
		       out.close();
		       
			   fos = new FileOutputStream(filePath.getAbsolutePath() + ".UML" );
		       out = new ObjectOutputStream(fos);
		       out.writeObject(UML_TreePanel.treeModel);
		       out.close();
	       }
        }
    	catch(IOException ex)
    	{
           ex.printStackTrace();
    	}
    }
    private void OpenProject() // This implimentation is so ridiculous :P. Works though
    {
    	FileInputStream fis = null;
    	ObjectInputStream in = null;
    	try
    	{
    		
    		fc = new JFileChooser();
 	       	fc.setFileSelectionMode(JFileChooser.FILES_ONLY);
 	       	fc.setName("Open From File");
 	       	File filePath;
    		
 	       int returnVal = fc.showDialog(this , "Open File");
	       if (returnVal == JFileChooser.APPROVE_OPTION){
	    	   	filePath = fc.getSelectedFile();
	    	   	String projectName = filePath.getAbsolutePath().replace(".MaSE", "").replace(".UML", "");
		    	fis = new FileInputStream(projectName + ".MaSE");
		    	in = new ObjectInputStream(fis);
		    	DefaultTreeModel maseModel = (DefaultTreeModel)in.readObject();
		    	
		    	remove(WorkPanel);
		    	MaSE_Panel.remove(MaSE_TreePanel);
		    	MaSE_Panel.remove(MaSE_ViewerPanel);
		    	MaSE_TreePanel = new DynamicTree("MaSE Artifacts", maseModel);
		        //MaSE_TreePanel.setPreferredSize(new Dimension(150, 300));
		    	MaSE_TreePanel.setPreferredSize(new Dimension(400, 260));
		    	MaSE_Panel.add(MaSE_TreePanel, BorderLayout.WEST);	    	
		    	MaSE_Panel.add(MaSE_ViewerPanel, BorderLayout.EAST);
		        //add(MaSE_Panel, BorderLayout.WEST);
		    	
		        MouseListener m1 = new MouseAdapter() {
		            public void mousePressed(MouseEvent e) {
		                int selRow = MaSE_TreePanel.tree.getRowForLocation(e.getX(), e.getY());
		                TreePath selPath = MaSE_TreePanel.tree.getPathForLocation(e.getX(), e.getY());
		                if(selRow != -1) {
		                    if(e.getClickCount() == 1) {
		                        //mySingleClick(selRow, selPath); DO Nothing
		                    }
		                    else if(e.getClickCount() == 2) {
		                        myMaSEDoubleClick(selRow, selPath);
		                    }
		                }
		            }
		        };
		        
		        MaSE_TreePanel.tree.addMouseListener(m1);
		        
		        
		        fis = new FileInputStream(projectName + ".UML");
		    	in = new ObjectInputStream(fis);
		    	DefaultTreeModel umlModel = (DefaultTreeModel)in.readObject();
		    	UML_Panel.remove(UML_TreePanel);
		    	UML_TreePanel = new DynamicTree("UML Artifacts", umlModel);
		        //UML_TreePanel.setPreferredSize(new Dimension(150, 300));
		        UML_TreePanel.setPreferredSize(new Dimension(400,260));
		        UML_Panel.add(UML_TreePanel, BorderLayout.WEST);
		        UML_Panel.add(UML_ViewerPanel, BorderLayout.EAST);
		        //add(UML_Panel, BorderLayout.EAST);
		        
		        MouseListener m2 = new MouseAdapter() {
		            public void mousePressed(MouseEvent e) {
		                int selRow = UML_TreePanel.tree.getRowForLocation(e.getX(), e.getY());
		                TreePath selPath = UML_TreePanel.tree.getPathForLocation(e.getX(), e.getY());
		                if(selRow != -1) {
		                    if(e.getClickCount() == 1) {
		                        //mySingleClick(selRow, selPath); DO Nothing
		                    }
		                    else if(e.getClickCount() == 2) {
		                        myUMLDoubleClick(selRow, selPath);
		                    }
		                }
		            }
		        };
		        
		        UML_TreePanel.tree.addMouseListener(m2);
		        
		        
		        WorkPanel = new JPanel(new GridLayout(2,0)); 
		        WorkPanel.add(MaSE_Panel, BorderLayout.NORTH);
		        WorkPanel.add(UML_Panel, BorderLayout.SOUTH);
		        
		        add(WorkPanel, BorderLayout.CENTER);
		        
		        //Refresh the main frame
		        setVisible(false);
		        setVisible(true);
		        in.close();
		        //sequenceMenuItem.setEnabled(true); Broken Broken Broken
	       }
    	}
    	catch(IOException ex)
    	{
    	ex.printStackTrace();
    	}
    	catch(ClassNotFoundException ex)
    	{
    	ex.printStackTrace();
    	}   	
    }
    private File generateSequence(String MaSEfilePath)
    {
    	File sequenceFile = null;
    	String MaSEfilename = new File(MaSEfilePath).getName();
		
		//First of all create an instance to the import manager
		UMLSequenceImportManager importer = new UMLSequenceImportManager();
		
		//Then create an instance of the Message data structure. Basically an arraylist of messages. We'll
		//use this later to hold arraylist of messages returned by other functions
		ArrayList<Message> m = new ArrayList<Message>();
		
		
		//import the files. Note that order does not matter
		//BUT ONE AND ONLY ONE AgentDiagram file should be imported
		//ideally the user, through the GUI, should create a new project and the project
		//will require the importing of the agent file first. Then, any number of protocol files
		//can be imported
		
		//tell the import manager to import a protocol file from a given location
		//tell the import manager to import an agent file at a given location
		try {
			importer.import_protocol_file(MaSEfilePath);
			importer.import_agent_file(classDiagramFilepath);
			//tell the import manager to import another protocol file from a given location
			//importer.import_protocol_file("img/ProtocolDiagram2.protocol");		
			
			//create an instance uml artifact manager
			UMLSequenceDomainModeler umlArtifacts = new UMLSequenceDomainModeler();		
			
			//from now on for simplicity's sake no files should be added in

			//pass the list of protocols from the importer to the UML artifact manager
			umlArtifacts.set_theProtocolMessageList(importer.get_theProtocolMessageList());		
			//pass the list of agents and their roles from the importer to the UML artifact manager
			umlArtifacts.set_theAgentList(importer.get_theAgentList());
			

			//the umlArtifacts manager prints the list of agents and their roles
			//demonstrating a helper function
			umlArtifacts.print_theAgentList();

			//print the list of protocol messages for each file
			//demonstrating a helper function 
			umlArtifacts.print_theProtocolMessageList();
			
			try
			{
				umlArtifacts.transform_messageList(MaSEfilePath);			
				m=umlArtifacts.get_theMessageListOfFile(MaSEfilePath);			
				umlArtifacts.print_theMessageList(m);
				UMLSequenceConversionManager inst = new UMLSequenceConversionManager();
				sequenceFile = inst.createSequenceDiagram(m, workingDirectory + File.separator + MaSEfilename + ".violet");
				m=null;
				mumConsole.append("Created: " + workingDirectory + File.separator + MaSEfilename + ".violet");
				UML_TreePanel.addObject( p6 , workingDirectory + File.separator + MaSEfilename + ".violet");
			} catch (Exception e)
			{
				System.out.println("Cannot transform the message list.");
			}
		} catch (IOException e1) {
			// TODO Auto-generated catch block
			mumConsole.append("\n" + e1.getMessage());
		} catch (JDOMException e1) {
			// TODO Auto-generated catch block
			mumConsole.append("\n" + e1.getMessage());
		}

		return sequenceFile;
    }
    
    
    
    
    /**
     * Create the GUI and show it.  For thread safety,
     * this method should be invoked from the
     * event-dispatching thread.
     */
    private static void createAndShowGUI() {
        //Create and set up the window.
        frame = new JFrame("MAS UML MODELER");
        frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);

        //Create and set up the content pane.
        MainApplicationFrame newContentPane = MainApplicationFrame.getInstance();
        frame.setJMenuBar(newContentPane.createMenuBar());
        newContentPane.setOpaque(true); //content panes must be opaque
        frame.setContentPane(newContentPane);

        //Display the window.
        frame.pack();
        frame.setVisible(true);
    }

    public static void create(String[] args) {
        //Schedule a job for the event-dispatching thread:
        //creating and showing this application's GUI.
    	MainApplicationFrame.getInstance().workingDirectory = args[1];
    	javax.swing.SwingUtilities.invokeLater(new Runnable() {
            public void run() {
                createAndShowGUI();
//        		//an example of flow through the functions
//        		
//        		//First of all create an instance to the import manager
//        		ImportManager importer = new ImportManager();
//        		
//        		//Then create an instance of the Message data structure. Basically an arraylist of messages. We'll
//        		//use this later to hold arraylist of messages returned by other functions
//        		ArrayList<Message> m = new ArrayList<Message>();
//        		
//        		
//        		//import the files. Note that order does not matter
//        		//BUT ONE AND ONLY ONE AgentDiagram file should be imported
//        		//ideally the user, through the GUI, should create a new project and the project
//        		//will require the importing of the agent file first. Then, any number of protocol files
//        		//can be imported
//        		
//        		//tell the import manager to import a protocol file from a given location
//        		importer.import_protocol_file("img/ProtocolDiagram1.protocol");
//        		//tell the import manager to import an agent file at a given location
//        		importer.import_agent_file("img/AgentDiagram2.agent");				
//        		//tell the import manager to import another protocol file from a given location
//        		importer.import_protocol_file("img/ProtocolDiagram2.protocol");		
//        		
//        		//create an instance uml artifact manager
//        		UMLArtifactManager umlArtifacts = new UMLArtifactManager();		
//        		
//        		//from now on for simplicity's sake no files should be added in
//
//        		//pass the list of protocols from the importer to the UML artifact manager
//        		umlArtifacts.set_theProtocolMessageList(importer.get_theProtocolMessageList());		
//        		//pass the list of agents and their roles from the importer to the UML artifact manager
//        		umlArtifacts.set_theAgentList(importer.get_theAgentList());
//        		
//
//        		//the umlArtifacts manager prints the list of agents and their roles
//        		//demonstrating a helper function
//        		umlArtifacts.print_theAgentList();
//
//        		//print the list of protocol messages for each file
//        		//demonstrating a helper function 
//        		umlArtifacts.print_theProtocolMessageList();
//        		
//        		//demonstrating another helper function
//        		//Given a filepath of an imported file, get the message list structure associated with it 
//        		//the catch statement is for the case where the filepath does not exist
//        		try
//        		{				
//        			//this function is used to return a message list of a certain filepath
//        			//using try/catch
//        			m=umlArtifacts.get_theMessageListOfFile("img/ProtocolDiagram1.protocol");	
//        			umlArtifacts.print_theMessageList(m);
//        			m=null;
//        		} catch(Exception e)
//        		{
//        			System.out.println("Can't find specified file.");
//        		}
//
//        		//Given a filepath of an imported file, get the message list structure associated with it 
//        		//the catch statement is for the case where the filepath does not exist
//        		try
//        		{
//        			//this function is used to return a message list of a certain filepath
//        			m=umlArtifacts.get_theMessageListOfFile("img/ProtocolDiagram2.protocol");			
//        			umlArtifacts.print_theMessageList(m);
//        			m=null;
//        		} catch (Exception e)
//        		{
//        			System.out.println("Can't find specified file.");
//        		}
//        		
//        		//Given a filepath of an imported file, get the message list structure associated with it 
//        		//the catch statement is for the case where the filepath does not exist
//        		try
//        		{
//        			//this function is used to return a message list of a certain filepath
//        			m=umlArtifacts.get_theMessageListOfFile("asdfasdfas");			
//        			umlArtifacts.print_theMessageList(m);
//        		}
//        		catch (Exception e)
//        		{
//        			System.out.println("Can't find specified file.");
//        		}
//        		
//        		
//        		//transforming a given message list by swapping roles with agent names
//        		//Given a filepath of an imported file, transform the message list structure associated with it 
//        		//by swapping the roles with agent names
//        		//the catch statement is for the case where the filepath does not exist
//        		//In this case the list is printed as well
//        		try
//        		{
//        			umlArtifacts.transform_messageList("img/ProtocolDiagram1.protocol");			
//        			m=umlArtifacts.get_theMessageListOfFile("img/ProtocolDiagram1.protocol");			
//        			umlArtifacts.print_theMessageList(m);
//        			m=null;
//        		} catch (Exception e)
//        		{
//        			System.out.println("Cannot transform the message list");
//        		}
//        		
//        		try
//        		{
//        			umlArtifacts.transform_messageList("img/ProtocolDiagram2.protocol");			
//        			m=umlArtifacts.get_theMessageListOfFile("img/ProtocolDiagram2.protocol");			
//        			umlArtifacts.print_theMessageList(m);
//        			ConversionManager inst = new ConversionManager();
//        			//inst.createSequenceDiagram(m, "C:\\Documents and Settings\\halsayed\\Desktop\\FileName.violet");
//        			inst.createSequenceDiagram(m, "Must Be Valid FileName");
//        			m=null;
//        		} catch (Exception e)
//        		{
//        			System.out.println("Cannot transform the message list.");
//        		}
//        		
//        		try
//        		{
//        			umlArtifacts.transform_messageList("asdfasdfassaf");			
//        			m=null;
//        		} catch (Exception e)
//        		{
//        			System.out.println("Cannot transform the message list");
//        		}
//        		
//        		//At this point, the transformed message list can be sent to the module that goes through it
//        		//and generates the Violent Input
                
                
                
                
            }
        });
    }
}
